// pages/index/web/index.js
import Util from '../../../utils/utils';
Page({
  autoAuth:2,
  /**
   * 页面的初始数据
   */
  isGoTo:false,
  refreshIndex:1,
  data: {
    url:'',
  },
  initPage:function(options){
    let url = '';
    this.refreshIndex = Util.getRandom(1000,9999);
    wx.hideShareMenu();
    if(options.hasOwnProperty('url')){
      url = decodeURIComponent(options.url);
      let token = Util.token;
      if(url.indexOf('token') === -1){
        if(url.indexOf('?')> -1){
          url += '&token=' + token;
        }else{
          url += '?token=' + token;
        }
      }
      if(url.indexOf('?')> -1){
        url += '&ts=' + this.refreshIndex;
      }else{
        url += '?ts=' + this.refreshIndex;
      }
      if(url.indexOf('#') === -1){
        url += '#wechat_redirect';
      }

      if(options.hasOwnProperty('title')){
        options.title = decodeURIComponent(options.title);
        wx.setNavigationBarTitle({
          title:options.title
        });
      }
      console.log(url);
      this.setData({
        url:url
      });
    }else{
      Util.msg('网址错误!');
      setTimeout(function () {
        wx.navigateBack();
      },500);
    }
  },
  onShow:function(){
    // if(this.isGoTo === true){
    //   let url = this.data.url;
    //   url = url.replace('tm=' + this.refreshIndex,'tm=' + (this.refreshIndex + 1));
    //   this.setData({
    //     url:url
    //   });
    //   this.refreshIndex = this.refreshIndex + 1;
    //   console.log('刷新网页！！',url);
    //   this.isGoTo = false;
    // }
  },
  onHide:function(){
    this.isGoTo = true;
  },
  /**
   * 下拉刷新页面数据
   * @returns {boolean}
   */
  onPullDownRefresh: function(){
    this.initPage(true);
  },
  postMessage:function (event) {
    /**
     * 后退时主动更新用户资料
     */
    console.log('触发消息',event);
    this.getUserInfo(function (result) {
    },false,true);
  },
  loadSuccess:function (event) {

  },
  loadError:function (event) {

  },
});
